﻿
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.Base;
using SmartWMS.Domain.SystemSet;
using SmartWMS.EntityFramework;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.Dictionary.Dto;

namespace SmartWMS.Server.SystemSets.Dictionary
{
    public class DictionarySerivce : IDictionarySerivce
    {
        private readonly IRepository<DicType> _dictyperepository;

        private readonly IRepository<DictItem> _dictitemrepository;

        private readonly SmartWMSContext _context;

        public DictionarySerivce(IRepository<DicType> dictyperepository, IRepository<DictItem> dictitemrepository, SmartWMSContext context)
        {
            _dictyperepository = dictyperepository;
            _dictitemrepository = dictitemrepository;
            _context = context;
        }

        /// <summary>
        /// 添加公共字典
        /// </summary>
        /// <param name="systemSetsInput"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddSystemSet(SystemSetsInput systemSetsInput)
        {
            var resault = systemSetsInput.MapTo<DicType>();

            var addlist = await _dictyperepository.InsertAsync(resault);

            return addlist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 公共字典列表
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public async Task<Response<List<SystemSetsInput>>> GetSStaffListAsync(string? typeName)
        {
            var resault = await _dictyperepository.GetAllListAsync();

            if (!string.IsNullOrEmpty(typeName))
            {
                resault = resault.Where(x => x.TypeName.Contains(typeName)).ToList();
            }

            var dictlist = resault.MapToList<SystemSetsInput>();

            return Response<List<SystemSetsInput>>.Ok(dictlist);
        }

        /// <summary>
        /// 添加字典明细
        /// </summary>
        /// <param name="systemSetsDto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddSystemSetDetail(SystemSetsDto systemSetsDto)
        {
            var resault = systemSetsDto.MapTo<DictItem>();

            var addlist = await _dictitemrepository.InsertAsync(resault);

            return addlist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 字典明细列表
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public async Task<Response<List<SystemSetsDto>>> GetResponseListAsync(SystemSetsDto Info)
        {
            var resault = await _dictitemrepository.GetAllListAsync();

            if (!string.IsNullOrEmpty(Info.ItemName))
            {
                resault = resault.Where(x => x.ItemName.Contains(Info.ItemName)).ToList();
            }

            if (Info.Id != 0)
            {
                resault = resault.Where(x => x.TypeId == Info.Id).ToList();
            }

            var dictlist = resault.MapToList<SystemSetsDto>();

            return Response<List<SystemSetsDto>>.Ok(dictlist);
        }

        /// <summary>
        /// 反填字典明细
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public async Task<Response<SystemSetsDto>> GetSystemSetDetailAsync(KeyEntity Info)
        {
            var resault = await _dictitemrepository.GetEntityByIdAsync(new KeyEntity { Id = Info.Id });

            var dictlist = resault.MapTo<SystemSetsDto>();

            return Response<SystemSetsDto>.Ok(dictlist);
        }

        /// <summary>
        /// 反填公共字典
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public async Task<Response<SystemSetsInput>> GetSystemSetAsync(KeyEntity Info)
        {
            var resault = await _dictyperepository.GetEntityByIdAsync(new KeyEntity { Id = Info.Id });

            var dictlist = resault.MapTo<SystemSetsInput>();

            return Response<SystemSetsInput>.Ok(dictlist);
        }

        /// <summary>
        /// 修改公共字典
        /// </summary>
        /// <param name="systemSetsInput"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateSystemSet(SystemSetsInput systemSetsInput)
        {
            var resault = systemSetsInput.MapTo<DicType>();

            var updatelist = await _dictyperepository.UpdateAsync(resault);

            return updatelist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 修改字典明细
        /// </summary>
        /// <param name="systemSetsDto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateSystemSetDetail(SystemSetsDto systemSetsDto)
        {
            var resault = systemSetsDto.MapTo<DictItem>();

            var updatelist = await _dictitemrepository.UpdateAsync(resault);

            return updatelist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 状态修改
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateState(SystemSetsInput Info)
        {
            var resault = await _dictyperepository.GetEntityByIdAsync(new KeyEntity { Id = Info.Id });

            resault.State = !resault.State;

            var updatelist = _context.SaveChanges();

            return updatelist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 字典明细状态修改
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateDetailState(SystemSetsDto Info)
        {
            var resault = await _dictitemrepository.GetEntityByIdAsync(new KeyEntity { Id = Info.Id });

            resault.State = !resault.State;

            var updatelist = _context.SaveChanges();

            return updatelist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }
        
    }
}
